package cn.iocoder.yudao.module.member.service.scorelevel;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.scorelevel.vo.ScoreLevelCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.scorelevel.vo.ScoreLevelExportReqVO;
import cn.iocoder.yudao.module.member.controller.admin.scorelevel.vo.ScoreLevelPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.scorelevel.vo.ScoreLevelUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.scorelevel.ScoreLevelConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.scorelevel.ScoreLevelDO;
import cn.iocoder.yudao.module.member.dal.mysql.scorelevel.ScoreLevelMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.SCORE_LEVEL_NOT_EXISTS;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.SCORE_TYPE_NOT_EXISTS;

/**
 * 积分等级 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ScoreLevelServiceImpl implements ScoreLevelService {

    @Resource
    private ScoreLevelMapper scoreLevelMapper;

    @Override
    public Long createScoreLevel(ScoreLevelCreateReqVO createReqVO) {
        // 插入
        ScoreLevelDO scoreLevel = ScoreLevelConvert.INSTANCE.convert(createReqVO);
        scoreLevelMapper.insert(scoreLevel);
        // 返回
        return scoreLevel.getId();
    }

    @Override
    public void updateScoreLevel(ScoreLevelUpdateReqVO updateReqVO) {
        // 校验存在
        validateScoreLevelExists(updateReqVO.getId());
        // 更新
        ScoreLevelDO updateObj = ScoreLevelConvert.INSTANCE.convert(updateReqVO);
        scoreLevelMapper.updateById(updateObj);
    }

    @Override
    public void deleteScoreLevel(Long id) {
        // 校验存在
        validateScoreLevelExists(id);
        // 删除
        scoreLevelMapper.deleteById(id);
    }

    private void validateScoreLevelExists(Long id) {
        if (scoreLevelMapper.selectById(id) == null) {
            throw exception(SCORE_LEVEL_NOT_EXISTS);
        }
    }

    @Override
    public ScoreLevelDO getScoreLevel(Long id) {
        return scoreLevelMapper.selectById(id);
    }

    @Override
    public List<ScoreLevelDO> getScoreLevelList(Collection<Long> ids) {
        return scoreLevelMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ScoreLevelDO> getScoreLevelPage(ScoreLevelPageReqVO pageReqVO) {
        return scoreLevelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ScoreLevelDO> getScoreLevelList(ScoreLevelExportReqVO exportReqVO) {
        return scoreLevelMapper.selectList(exportReqVO);
    }

}
